﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace SVNUG.Presentations.NoSql.Business
{
    public class ExampleInitializer
    {
        public void Initialize()
        {
            var total = RavenDbContext.Instance.FindAllOrders();

            //clear out old data
            foreach (var customer in RavenDbContext.Instance.FindAllCustomers())
            {
                RavenDbContext.Instance.RemoveCustomer(customer);
            }
            foreach (var order in RavenDbContext.Instance.FindAllOrders())
            {
                RavenDbContext.Instance.RemoveOrder(order);
            }


            this.InitializeCustomers();

            this.InitializeOrders(10,12);
        }
        
        private void InitializeCustomers()
        {
            Customer customer = new Customer()
            {
                FirstName = "Glenn",
                LastName = "Quagmire",
                UserName = "Giggidy",
                Password = "Allright",
                Photo = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "SVNUG.Presentations.NoSql.Business.quagmire.jpg").ToByteArray(),
                AddressOnFile = new Address()
                {
                    Address1 = "123 Spooner street",
                    Address2 = "",
                    City = "Quahog",
                    Name = "Quacky",
                    State = "RI",
                    Zip = "90210"
                }
            };
            RavenDbContext.Instance.StoreCustomer(customer);

            Customer customer2 = new Customer()
            {
                FirstName = "Peter",
                LastName = "Griffin",
                UserName = "Password",
                Password = "Password",
                Photo = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "SVNUG.Presentations.NoSql.Business.peter.gif").ToByteArray(),
                AddressOnFile = new Address()
                {
                    Address1 = "122 Spooner street",
                    Address2 = "",
                    City = "Quahog",
                    Name = "",
                    State = "RI",
                    Zip = "90210"
                }
            };
            RavenDbContext.Instance.StoreCustomer(customer2);

            Customer customer3 = new Customer()
            {
                FirstName = "Joe",
                LastName = "Swanson",
                UserName = "Cop",
                Password = "bacon",
                Photo = System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceStream(
                    "SVNUG.Presentations.NoSql.Business.joe.jpg").ToByteArray(),
                AddressOnFile = new Address()
                {
                    Address1 = "124 Spooner street",
                    Address2 = "",
                    City = "Quahog",
                    Name = "",
                    State = "RI",
                    Zip = "90210"
                }
            };
            RavenDbContext.Instance.StoreCustomer(customer3);


        }
        private void InitializeOrders(Int32 glennsOrders,Int32 petersOrders)
        {
            Random randy = new Random();

            var glenn = RavenDbContext.Instance.TryFindCustomerByLastName("Quagmire");
            for (Int32 i = 0; i < glennsOrders; i++)
            {
                var order = new Order()
                {
                    CustomerId = glenn.CustomerId,
                    DateOrdered = DateTime.Now.AddDays(-i),
                    ItemList = new OrderItemList()
                };

                order.ItemList.Add(new OrderItem()
                {
                    Upc = "DEB#" + i,
                    Description = String.Format("Debbie does Dallas #{0}", i),
                    Price = 29.99M
                });

                if (order.DateOrdered < DateTime.Now.AddDays(-5))
                {
                    order.Shipping = new ShippingInformation()
                    {
                        DateShipped = order.DateOrdered.AddDays(2 + randy.Next(0, 3)),
                        FromAddress = new Address()
                        {
                            Address1 = "Lil Debbie productions",
                            Address2 = "",
                            City = "Hollywood",
                            Name = "Good times industries",
                            State = "CA",
                            Zip = "90210"
                        },
                        ShippingCarrier = (ShippingCarrier)randy.Next(0, 4),
                        ShippingCost = 4.76M,
                        ToAddress = glenn.AddressOnFile,
                        TrackingNumber = new Guid(i, 0, 0, new byte[8]).ToString()
                    };
                }

                RavenDbContext.Instance.StoreOrder(order);
            }


            var peter = RavenDbContext.Instance.TryFindCustomerByLastName("griffin");
            for (Int32 i = 0; i < petersOrders; i++)
            {
                var order = new Order()
                {
                    CustomerId = peter.CustomerId,
                    DateOrdered = DateTime.Now.AddDays(-i),
                    ItemList = new OrderItemList()
                };

                order.ItemList.Add(new OrderItem()
                {
                    Upc = "DUMM#" + i,
                    Description = String.Format("Addition for dummies #{0}", i),
                    Price = 12.99M
                });

                if (order.DateOrdered < DateTime.Now.AddDays(-5))
                {
                    order.Shipping = new ShippingInformation()
                    {
                        DateShipped = order.DateOrdered.AddDays(2 + randy.Next(0, 3)),
                        FromAddress = new Address()
                        {
                            Address1 = "Doh",
                            Address2 = "",
                            City = "st louis",
                            Name = "idunno",
                            State = "mo",
                            Zip = "43213"
                        },
                        ShippingCarrier = (ShippingCarrier)randy.Next(0, 4),
                        ShippingCost = 2.76M,
                        ToAddress = peter.AddressOnFile,
                        TrackingNumber = new Guid(i, 0, 0, new byte[8]).ToString()
                    };
                }

                RavenDbContext.Instance.StoreOrder(order);
            }
        }

        
    }

    public static class Extensions
    {
        public static Byte[] ToByteArray(this Stream s)
        {
            Byte[] buffer = new Byte[s.Length];

            s.Read(buffer, 0, Convert.ToInt32(s.Length));

            return buffer;
        }
    }
}
